/**
 * Create By FengZheng
 * On 2019/11/5 6:25
 */

/**
 * 查找当天课程信息
 */
(function ($) {
    "use strict";

    let $dom = {
        $classContainer: $("#class-container"),
        $clockContainer: $("#time-container")
    };


    /**
     * 查找当天课程信息
     */
    function queryCurrentClassInfo() {
        $.ajax({
            url: "/admin/frame/query-user-current-class-info",
            type: "post",
            dataType: "json",
            success: function (resultD) {
                if( (typeof resultD)==="object" ){
                    let status = resultD.status;
                    if(status === 200){
                        let info = resultD.info;

                        if( (typeof info)==="string" ){
                            info = JSON.parse(info);
                        }

                        let classArray = [];
                        for(let i=0,len=info.length; i<len; i++){

                            // 学生真实姓名判断
                            let studentName = info[i].studentName;
                            if(info[i].studentRealName !== null && info[i].studentRealName !== ""){
                                studentName = info[i].studentRealName;
                            }

                            // 教师真实姓名判断
                            let teacherName = info[i].teacherName;
                            if(info[i].teacherRealName != null && info[i].teacherRealName !== ""){
                                teacherName = info[i].teacherRealName;
                            }

                            // 家长真实姓名判断
                            let parentsName = info[i].parentsName;
                            if(info[i].parentsRealName != null && info[i].parentsRealName !== ""){
                                parentsName = info[i].parentsRealName;
                            }

                            // 上课时间
                            let classTime = info[i].classTime;

                            let classData = {
                                "subjectName": info[i].subjectName,
                                "className": info[i].className,
                                "teacherName": teacherName,
                                "studentName": studentName,
                                "parentsName": parentsName,
                                "classTime": classTime,
                                "classId": info[i].classId
                            };

                            classArray.push(classData);
                        }

                        // 开始输出
                        doOutput(classArray);
                    }
                }
            }
        });
    }

    /**
     * 遍历输出信息
     */
    function doOutput(classArray) {
        if(classArray.length !== 0){
            let timeStamp = $dom.$clockContainer.attr("data-time").trim();

            for(let i=0,len=classArray.length; i<len; i++){
                let cInfo = classArray[i],
                    classTime = cInfo.classTime,
                    hasClass = false;

                // 还未开始上课
                if(classTime > timeStamp){
                    hasClass = true;
                }else {
                    let betweenTime = timeStamp - classTime;
                    if(betweenTime < 720000){
                        hasClass = true;
                    }
                }

                let content = "";
                if(hasClass){
                    content = "<tr>"+
                                    "<td>"+ parseInt(i)+1 +"</td>"+
                                    "<td class=\"td-course\">"+ cInfo.subjectName +"</td>"+
                                    "<td>"+ cInfo.className +"</td>"+
                                    "<td>"+ cInfo.teacherName +"</td>"+
                                    "<td>"+ cInfo.studentName +"</td>"+
                                    "<td>"+ cInfo.parentsName +"</td>"+
                                    "<td></td>"+
                                    "<td>"+ formatTime(classTime) +"</td>"+
                                    "<td></td>"+
                                    "<td>"+
                                        "<button data-id=\""+ cInfo.classId +"\" name=\"join-class\" type=\"button\" class=\"btn btn-default\">进入教室</button>"+
                                    "</td>"+
                                "</tr>";
                    $dom.$classContainer.append(content);
                    bindClickEvent();
                }
            }
        }
    }

    /**
     * 绑定事件
     */
    function bindClickEvent() {
        $("[name='join-class']").on("click",function (ev) {
            let event = ev || window.event;
            let target = event.target || event.srcElement;

            let classId = $(target).attr("data-id");

            let mWindow = window.open("","_blank","width=500,height=500");
            mWindow.document.write("ID:"+classId);
        });
    }

    /**
     * 时间格式化
     */
    function formatTime(longTime){
        let date = new Date(longTime);

        let year = date.getFullYear(),
            month = date.getMonth()+1,
            day = date.getDate(),
            hour = date.getHours(),
            minute = date.getMinutes();

        month = month < 10 ? "0"+month : month;
        day = day < 10 ? "0"+day : day;
        hour = hour < 10 ? "0"+hour : hour;
        minute = minute < 10 ? "0"+minute : minute;

        return year +"-"+ month +"-"+ day +" "+ hour +":"+minute+":00";
    }

    $(document).ready(function () {
        queryCurrentClassInfo();
    });
})(window.jQuery);